package com.fanbuchi.app.ins.net;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.fanbuchi.app.ins.app.Started;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;

/**
 * Created by os on 16/5/12.
 * Title
 *  BasicAuthorizationInterceptor basicAuthorizationInterceptor = new BasicAuthorizationInterceptor();
    mOkHttpClient.interceptors().add(basicAuthorizationInterceptor);
 */
public class BasicAuthorizationInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request basicAuthRequest=null;
        try {
            ApplicationInfo appInfo = Started.getInstance().getPackageManager()
                    .getApplicationInfo(Started.getInstance().getPackageName(),PackageManager.GET_META_DATA);

            String appid=appInfo.metaData.getString("xgggh_appid");
            String appsecret=appInfo.metaData.getString("xgggh_secret");

            String credential = Credentials.basic(appid,appsecret);
            basicAuthRequest =request.newBuilder()
                    .header("Authorization", credential)
                    .build();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return chain.proceed(basicAuthRequest);
    }

}
